﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using System.Web.Mvc;
namespace Services
{

    public partial class OrderServiceService
    {
        readonly IOrderServiceEntity entity;

        public OrderServiceService(IOrderServiceEntity entity)
        {
            this.entity = entity;

        }

        #region Main Method

        /// <summary>
        /// Save 
        /// </summary>
        /// <param name="_model"></param>
        /// <returns></returns>
        public long Save(object _model)
        {
            try
            {
                var obj = (CoreData.OrderService)_model;
                var id = obj.ID;
                if (id == 0)
                {

                    return entity.Save(obj, Table.OrderService.ToString());
                }
                else
                {
                    return entity.Update(obj, Table.OrderService.ToString());
                }
            }
            catch { return -1; }
        }

        /// <summary>
        /// Get by ID
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public CoreData.OrderService GetByID(long _id)
        {
            try
            {
                return entity.GetById(_id, Table.OrderService.ToString());
            }
            catch { return null; }
        }

        /// <summary>
        /// Get List
        /// </summary>
        /// <returns></returns>
        public IList<CoreData.OrderService> GetList()
        {
            try
            {
                return entity.GetAll(Table.OrderService.ToString()).ToList();
            }
            catch { return null; }

        }

        /// <summary>
        /// Get List with Linq
        /// </summary>
        /// <returns></returns>
        public IList<CoreData.OrderService> GetListByLINQ(Func<CoreData.OrderService, Boolean> _where)
        {
            try
            {
                return entity.GetMany(_where, Table.OrderService.ToString()).ToList();
            }
            catch { return null; }
        }
        /// <summary>
        /// Get One with Linq
        /// </summary>
        /// <returns></returns>
        public CoreData.OrderService GetOneByLINQ(Func<CoreData.OrderService, Boolean> _where)
        {
            try
            {
                return entity.Get(_where, Table.OrderService.ToString());
            }
            catch { return null; }
        }

        /// <summary>
        /// Get List with search string
        /// </summary>
        /// <returns></returns>
        public IList<CoreData.OrderService> GetList(string _searchstring)
        {
            //search theo tieu chi nao do         
            try
            {
                return entity.GetBySearchString(_searchstring, Table.OrderService.ToString()).ToList();
            }
            catch { return null; }
        }


        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="_model"></param>
        /// <returns></returns>
        public bool Delete(object _model)
        {
            try
            {
                entity.Delete((CoreData.OrderService)_model, Table.OrderService.ToString());
                return true;
            }
            catch { return false; }
        }


        #endregion



        #region Other Method

        public IList<CoreData.OrderService> GetByTypeStatus(int status, int typeid)
        {
            try
            {
                var list = entity.GetMany(c => c.Status.Equals(status) && c.Type == typeid, Table.OrderService.ToString()).ToList();
                return list;
            }
            catch
            {
                return null;
            }
        }
        public List<SelectListItem> GetListStatus(int status)
        {
            List<SelectListItem> stt = new List<SelectListItem>();
            SelectListItem _new = new SelectListItem() { Text = "Đơn hàng mới", Value = "1" };
            SelectListItem _pending = new SelectListItem() { Text = "Đang xử lý", Value = "2" };
            SelectListItem _complete = new SelectListItem() { Text = "Hoàn thành", Value = "3" };
            SelectListItem _paid = new SelectListItem() { Text = "Đã thanh toán", Value = "4" };
            SelectListItem _cancel = new SelectListItem() { Text = "Yêu cầu hủy", Value = "5" };

            stt.Add(_new); stt.Add(_pending); stt.Add(_complete); stt.Add(_paid); stt.Add(_cancel);
            foreach (var c in stt)
            {
                if (status != 0)
                    c.Selected = c.Value == status.ToString();
            }
            return stt;
        }
        #endregion

    }

}
